package cn.zjh.base.controller;

import cn.zjh.base.exception.BusinessException;
import cn.zjh.user.service.service.ILoginService;
import cn.zjh.base.utils.AjaxResult;
import cn.zjh.base.vo.LoginVo;
import cn.zjh.base.vo.WechatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
    * @Title: graph
    * @Description: 账号登录
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/6 16:24
    * @Parameters:
    * @Return
    */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginVo vo){
        try {
            //调取service账户登录方法,返回token/logininfo用户对象信息
            HashMap<String,Object> map = loginService.account(vo);
            //放入响应中进行返回
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }



    /**
    * @Title: wechat
    * @Description: 微信扫码登录
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/9 16:11
    * @Parameters:
    * @Return
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatVo vo){
        try {
            //调用service进行登录
            return loginService.wechat(vo);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: binder
    * @Description: 微信登录绑定
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/10 22:00
    * @Parameters:
    * @Return
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatVo vo){
        try {
            //调用service进行登录
            HashMap<String,Object> map = loginService.binder(vo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }




}
